unit cataurus.verwaltung.common.typeproject;

interface

uses
  SysUtils,
  IniFiles,
  Contnrs,
  Classes;

type
  TResourcenFile = class
  private
    FName: string;
    FMD5: string;
    FUpdate: Boolean;
    FBackupMD5: string;
  public
    constructor Create;
    property Name : string read FName write FName;
    property MD5 : string read FMD5 write FMD5;
    property Update : Boolean read FUpdate write FUpdate;
    property BackupMD5 : string read FBackupMD5 write FBackupMD5;
  end;

  TResourcenFiles = class(TObjectList)
  private
    function GetItem(Index: Integer): TResourcenFile;
    procedure SetItem(Index: Integer; const Value: TResourcenFile);
  public
    function Add(Name, MD5 : string) : integer; overload;
    function Add(AObject: TResourcenFile): Integer; overload;
    function IndexOf(AObject: TResourcenFile): Integer; overload;
    function IndexOf(Name : string): Integer; overload;
    function IndexForMD5(MD5 : string) : integer;
    function UpdateAll : integer;
    property Items[Index: Integer]: TResourcenFile read GetItem write SetItem; default;
  end;

  TProjectFile = class
  private
    FName: string;
    FSubDir: string;
    FIsErf: boolean;
    FUpdate: Boolean;
    FMD5: string;
    FResourcen: TResourcenFiles;
    FBackupMD5: string;
  public
    constructor Create;
    destructor Destroy; override;
    property SubDir : string read FSubDir write FSubDir;
    property Name : string read FName write FName;
    property MD5 : string read FMD5 write FMD5;
    property BackupMD5 : string read FBackupMD5 write FBackupMD5;
    property IsErf : boolean read FIsErf write FIsErf;
    property Update : Boolean read FUpdate write FUpdate;
    property Resourcen : TResourcenFiles read FResourcen write FResourcen;
  end;

  TProjectFiles = class(TObjectList)
  private
    function GetItem(Index: Integer): TProjectFile;
    procedure SetItem(Index: Integer; const Value: TProjectFile);
  public
    function Add(AObject: TProjectFile): Integer;
    function IndexOf(AObject: TProjectFile): Integer;
    function FindItem(SubDir, Name : string) : Integer;
    property Items[Index: Integer]: TProjectFile read GetItem write SetItem; default;
  end;

  TProject = Class
  private
    IniFile : TIniFile;
    AppIni : TIniFile;
    FFiles: TProjectFiles;
    function GetFTPDirectory: string;
    procedure SetFTPDirectory(const Value: string);
    function GetFTPHost: string;
    procedure SetFTPHost(const Value: string);
    function GetFTPBenutzer: string;
    function GetFTPPasswort: string;
    function GetFTPPort: integer;
    procedure SetFTPBenutzer(const Value: string);
    procedure SetFTPPasswort(const Value: string);
    procedure SetFTPPort(const Value: integer);
    function GetProjektName: string;
    procedure SetProjektName(const Value: string);
    function GetBeschreibung: string;
    procedure SetBeschreibung(const Value: string);
    function GetVerzeichnis: string;
    procedure SetVerzeichnis(const Value: string);
  public
    constructor Create(AFile : TFileName; AAppIni : TIniFile);
    destructor Destroy; override;
    function GetIndexForProject : integer;
    procedure ClearProjektFile;
    procedure LoadProjektFile;
    procedure SaveProjektFile;
    function GetKomplettCount : integer;
    property ProjektName : string read GetProjektName write SetProjektName;
    property Beschreibung : string read GetBeschreibung write SetBeschreibung;
    property Verzeichnis : string read GetVerzeichnis write SetVerzeichnis;
    property FTPDirectory : string read GetFTPDirectory write SetFTPDirectory;
    property FTPHost : string read GetFTPHost write SetFTPHost;
    property FTPPort : integer read GetFTPPort write SetFTPPort;
    property FTPBenutzer : string read GetFTPBenutzer write SetFTPBenutzer;
    property FTPPasswort : string read GetFTPPasswort write SetFTPPasswort;
    property Files : TProjectFiles read FFiles write FFiles;
  End;

implementation

{ TProject }

procedure TProject.ClearProjektFile;
var
  i: Integer;
  iAnzahlProjektFiles: Integer;
  iAnzahlProjektErfFiles : integer;
  k: Integer;
begin
  iAnzahlProjektFiles := IniFile.ReadInteger('Allgemein', 'Anzahl', 0);
  for i := 1 to iAnzahlProjektFiles do
  begin
    iAnzahlProjektErfFiles := IniFile.ReadInteger('File'+IntToStr(i), 'Anzahl', 0);
    if iAnzahlProjektErfFiles > 0 then
    begin
      for k := 1 to iAnzahlProjektErfFiles do
      begin
        IniFile.EraseSection('File'+IntToStr(i)+'_'+IntToStr(k));
      end;
    end;
    IniFile.EraseSection('File'+IntToStr(i));
  end;
end;

constructor TProject.Create(AFile: TFileName; AAppIni : TIniFile);
begin
  IniFile := TIniFile.Create(AFile);
  FFiles := TProjectFiles.Create;
  AppIni := AAppIni;
  LoadProjektFile;
end;

destructor TProject.Destroy;
begin
  ClearProjektFile;
  SaveProjektFile;
  FFiles.Free;
  IniFile.Free;
  inherited;
end;

function TProject.GetFTPDirectory: string;
begin
  result := AppIni.ReadString('Projekt'+ IntToStr(GetIndexForProject), 'FTPPfad', '');
end;

function TProject.GetBeschreibung: string;
begin
  result := IniFile.ReadString('Allgemein', 'Beschreibung', '');
end;

function TProject.GetFTPBenutzer: string;
begin
  result := AppIni.ReadString('Projekt'+ IntToStr(GetIndexForProject), 'FTPBenutzer', '');
end;

function TProject.GetFTPHost: string;
begin
  result := AppIni.ReadString('Projekt'+ IntToStr(GetIndexForProject), 'FTPHost', '');
end;

function TProject.GetFTPPasswort: string;
begin
  result := AppIni.ReadString('Projekt'+ IntToStr(GetIndexForProject), 'FTPPasswort', '');
end;

function TProject.GetFTPPort: integer;
begin
  result := AppIni.ReadInteger('Projekt'+ IntToStr(GetIndexForProject), 'FTPPort', 21);
end;

function TProject.GetIndexForProject: integer;
var
  iAnzahl, i : integer;
begin
  result := 0;
  iAnzahl := AppIni.ReadInteger('Allgemein', 'Anzahl', 0);
  for I := 1 to iAnzahl do
    begin
      if AppIni.ReadString('Projekt'+IntToStr(i), 'Name', '') = ProjektName
      then
        begin
          result := i;
          break;
        end;
    end;
end;

function TProject.GetKomplettCount: integer;
var
  i: Integer;
begin
  result := Files.Count;
  for i := 0 to Files.Count - 1 do
  begin
    result := result + Files[i].Resourcen.Count;
  end;
end;

function TProject.GetProjektName: string;
begin
  result := IniFile.ReadString('Allgemein', 'Name', '');
end;

function TProject.GetVerzeichnis: string;
begin
  result := IniFile.ReadString('Allgemein', 'Verzeichnis', '');
end;

procedure TProject.LoadProjektFile;
var
  i: Integer;
  iAnzahlProjektFiles: Integer;
  iAnzahlProjektErfFiles : integer;
  k: Integer;
  CurrentProjektFile : TProjectFile;
begin
  iAnzahlProjektFiles := IniFile.ReadInteger('Allgemein', 'Anzahl', 0);
  for i := 1 to iAnzahlProjektFiles do
  begin
    CurrentProjektFile := TProjectFile.Create;
    CurrentProjektFile.SubDir := IniFile.ReadString('File'+IntToStr(i), 'SubDir', '');
    CurrentProjektFile.Name := IniFile.ReadString('File'+IntToStr(i), 'Name', '');
    CurrentProjektFile.MD5 := IniFile.ReadString('File'+IntToStr(i), 'MD5', '');
    iAnzahlProjektErfFiles := IniFile.ReadInteger('File'+IntToStr(i), 'Anzahl', 0);
    CurrentProjektFile.IsErf := iAnzahlProjektErfFiles > 0;
    if iAnzahlProjektErfFiles > 0 then
    begin
      for k := 1 to iAnzahlProjektErfFiles do
      begin
        CurrentProjektFile.Resourcen.Add(
             IniFile.ReadString('File'+IntToStr(i)+'_'+IntToStr(k), 'Name', ''),
             IniFile.ReadString('File'+IntToStr(i)+'_'+IntToStr(k), 'MD5', '')
                                        );
      end;
    end;
    Files.Add(CurrentProjektFile);
  end;
end;

procedure TProject.SetFTPDirectory(const Value: string);
begin
  AppIni.WriteString('Projekt'+ IntToStr(GetIndexForProject), 'FTPPfad', Value);
end;

procedure TProject.SaveProjektFile;
var
  i: Integer;
  iAnzahlProjektFiles: Integer;
  iAnzahlProjektErfFiles : integer;
  k: Integer;
begin
  iAnzahlProjektFiles := Files.Count;
  for i := 1 to iAnzahlProjektFiles do
  begin
    IniFile.WriteString('File'+IntToStr(i), 'Name', Files[i-1].Name);
    IniFile.WriteString('File'+IntToStr(i), 'SubDir', Files[i-1].SubDir);
    IniFile.WriteString('File'+IntToStr(i), 'MD5', Files[i-1].MD5);

    iAnzahlProjektErfFiles := Files[i-1].Resourcen.Count;
    IniFile.WriteInteger('File'+IntToStr(i), 'Anzahl', iAnzahlProjektErfFiles);

    if iAnzahlProjektErfFiles > 0 then
    begin
      for k := 1 to iAnzahlProjektErfFiles do
      begin
        IniFile.WriteString('File'+IntToStr(i)+'_'+IntToStr(k), 'Name', Files[i-1].Resourcen[k-1].Name);
        IniFile.WriteString('File'+IntToStr(i)+'_'+IntToStr(k), 'MD5', Files[i-1].Resourcen[k-1].MD5);
      end;
    end;
  end;
  IniFile.WriteInteger('Allgemein', 'Anzahl', iAnzahlProjektFiles);
end;

procedure TProject.SetBeschreibung(const Value: string);
begin
  IniFile.WriteString('Allgemein', 'Beschreibung', Value);
end;

procedure TProject.SetFTPBenutzer(const Value: string);
begin
  AppIni.WriteString('Projekt'+ IntToStr(GetIndexForProject), 'FTPBenutzer', Value);
end;

procedure TProject.SetFTPHost(const Value: string);
begin
  AppIni.WriteString('Projekt'+ IntToStr(GetIndexForProject), 'FTPHost', Value);
end;

procedure TProject.SetFTPPasswort(const Value: string);
begin
  AppIni.WriteString('Projekt'+ IntToStr(GetIndexForProject), 'FTPPasswort', Value);
end;

procedure TProject.SetFTPPort(const Value: integer);
begin
  AppIni.WriteInteger('Projekt'+ IntToStr(GetIndexForProject), 'FTPPort', Value);
end;

procedure TProject.SetProjektName(const Value: string);
begin
  IniFile.WriteString('Allgemein', 'Name', Value);
end;

procedure TProject.SetVerzeichnis(const Value: string);
begin
  IniFile.WriteString('Allgemein', 'Verzeichnis', Value);
end;

{ TProjectFile }

constructor TProjectFile.Create;
begin
  inherited Create;
  FUpdate := false;
  FBackupMD5 := '';
  FResourcen := TResourcenFiles.Create;
end;

destructor TProjectFile.Destroy;
begin
  FResourcen.Free;
  inherited;
end;

{ TProjectFiles }

function TProjectFiles.Add(AObject: TProjectFile): Integer;
begin
  result := IndexOf(AObject);
  if (Result = -1)
  then
    result := inherited Add(AObject)
  else
    begin
      if Items[result].MD5 <> AObject.MD5
      then
        begin
          Items[result].MD5 := AObject.MD5;
          Items[result].Update := true;
          Items[result].Resourcen.Assign(AObject.Resourcen);
        end;
    end;
end;

function TProjectFiles.FindItem(SubDir, Name: string): Integer;
var
  Anzahl, i : integer;
begin
  result := -1;
  Anzahl := Count;
  for i := 0 to Anzahl - 1 do
  begin
    if (Items[i].Name = Name) and (Items[i].SubDir = SubDir)
    then
    begin
      result := i;
      break;
    end;
  end;
end;

function TProjectFiles.GetItem(Index: Integer): TProjectFile;
begin
  result := inherited Items[Index] as TProjectFile;
end;

function TProjectFiles.IndexOf(AObject: TProjectFile): Integer;
var
  Anzahl : integer;
  i: Integer;
begin
  Anzahl := Count;
  for i := 0 to Anzahl - 1 do
  begin
    if (Items[i].FName = AObject.Name) and (Items[i].SubDir = AObject.SubDir)
    then
      begin
        result := i;
        exit;
      end;
  end;
  result := -1;
end;

procedure TProjectFiles.SetItem(Index: Integer; const Value: TProjectFile);
begin
  inherited Items[Index] := Value;
end;

{ TResourcenFile }

constructor TResourcenFile.Create;
begin
  inherited;
  FUpdate := false;
end;

{ TResourcenFiles }

function TResourcenFiles.Add(Name, MD5: string): integer;
var
  ResourcenFile : TResourcenFile;
begin
  ResourcenFile := TResourcenFile.Create;
  ResourcenFile.MD5 := MD5;
  ResourcenFile.Name := Name;
  result := Add(ResourcenFile);
end;

function TResourcenFiles.Add(AObject: TResourcenFile): Integer;
var
  i : integer;
begin
  i := IndexOf(AObject);
  if i > -1 then
  begin
    if Items[i].MD5 <> AObject.MD5
    then
    begin
      Items[i].MD5 := AObject.MD5;
    end;
    result := i;
    AObject.Free;
  end else
  begin
    result := inherited Add(AObject);
  end;
end;

function TResourcenFiles.GetItem(Index: Integer): TResourcenFile;
begin
  result := inherited Items[Index] as TResourcenFile;
end;

function TResourcenFiles.IndexOf(AObject: TResourcenFile): Integer;
begin
  result := IndexOf(AObject.Name);
end;

function TResourcenFiles.IndexForMD5(MD5: string): integer;
var
  Anzahl, i : integer;
begin
  result := -1;
  Anzahl := Count;
  for i := 0 to Anzahl - 1 do
  begin
    if Items[i].MD5 = MD5 then
    begin
      result := i;
      break;
    end;
  end;
end;

function TResourcenFiles.IndexOf(Name : string): Integer;
var
  Anzahl, i : integer;
begin
  result := -1;
  Anzahl := Count;
  for i := 0 to Anzahl - 1 do
  begin
    if Items[i].Name = Name then
    begin
      result := i;
      break;
    end;
  end;
end;

procedure TResourcenFiles.SetItem(Index: Integer; const Value: TResourcenFile);
begin
  inherited Items[Index] := value;
end;

function TResourcenFiles.UpdateAll: integer;
var
  I: Integer;
begin
  for I := 0 to count - 1 do
  begin
    Items[i].Update := true;
  end;
  result := count;
end;

end.
